<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Refund;
use app\common\model\Lease as ModelLease;
use app\common\model\lease\Category;
use app\common\model\lease\Lease as LeaseLease;
use app\common\model\lease\Order;
use app\common\model\lease\Review;
use think\Db;
use think\Exception;
use think\Validate;

/**
 * 租赁接口
 */
class Lease extends Api
{
    protected $noNeedLogin = ['category', 'lists', 'info'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (租赁分类)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function category()
    {
        $list = Category::all(['pid' => 0]);
        $this->success('请求成功', $list);
    }

    /**
     * @Author LK
     * @ApiTitle (租赁列表)
     * @ApiMethod (GET)
     * @ApiParams (name="first_id", type="integer", required=true, sample="", description="一级分类")
     * @ApiParams (name="two_id", type="integer", required=true, sample="", description="二级分类")
     * @ApiParams (name="keywords", type="string", required=true, sample="", description="关键字")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function lists()
    {
        $param = request()->param();
        extract($param);
        $model = new LeaseLease();
        if (isset($keywords) && $keywords) {

            $model->whereLike('title', '%' . $keywords . '%');
        }
        if ($first_id && $two_id) {

            $model->where('c_id', $two_id);
        }
        if ($first_id && !$two_id) {

            // $cate_ids = Category::whereIn('pid', $first_id)->column('id');
            $model->where('f_id', $first_id);
        }
        $lists = $model->order('id desc')->paginate();
        $this->success('请求成功', $lists);
    }


    /**
     * @Author LK
     * @ApiTitle (租赁详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="id")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function info()
    {
        $param = request()->param();
        extract($param);
        $data = LeaseLease::get($id);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (租赁下单)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function addOrder()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $lease = LeaseLease::get($id);
        if (!$lease) {
            $this->error('产品不存在');
        }
        $lease->hidden(['id']);
        Db::startTrans();
        try {
            $data = $lease->toArray();
            $data['l_id'] = $id;
            $data['sn'] = sn('lease_order', 'sn', 'ZL');
            $data['user_id'] = $this->auth->id;
            $data['createtime'] = time();
            $order = Order::where('l_id', $id)->where('user_id', $this->auth->id)->where('status', '0')->find();
            if (!$order) {
                if ($lease->state == '0') {
                    exception('不可租');
                }
                $order = Order::create($data, true);
                if ($order) {
                    LeaseLease::where('id', $id)->update(['state' => '0']);
                }
                queue('app\common\job\LeaseOrder', $order->id, 300, '1255LeaseOrder');
            }

            Db::commit();
            $this->success('下单成功', $order->id);
        } catch (Exception $th) {
            //throw $th;
            Db::rollback();
            $this->error('下单失败', $th);
        }
    }
    /**
     * @Author LK
     * @ApiTitle (租赁订单)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function order_list()
    {
        $lists = Order::where('user_id', $this->auth->id)->where('status', '>', '0')->order('id desc')->paginate();
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (租赁订单详情)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function order_info()
    {
        $param = request()->param();
        extract($param);
        $data = Order::get($id);
        $this->success('', $data);
    }

    /**
     * @Author LK
     * @ApiTitle (租赁评论)
     * @ApiMethod (POST)
     * @ApiParams (name="order_id", type="integer", required=true, sample="", description="订单id")
     * @ApiParams (name="goods_id", type="integer", required=true, sample="", description="商品id")
     * @ApiParams (name="star", type="integer", required=true, sample="", description="星级")
     * @ApiParams (name="content", type="string", required=true, sample="", description="评论内容")
     * @ApiParams (name="images", type="string", required=true, sample="", description="评论图片")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function review()
    {
        $param = request()->param();
        extract($param);
        $validate = new Validate(
            [
                'order_id' => 'require|gt:0',
                'goods_id' => 'require|gt:0',
                'star' => 'require|gt:0',
                'content' => 'require',
            ],
            [
                'order_id.require' => '参数错误',
                'goods_id.require' => '参数错误',
                'star.require' => '请星级打分',
                'content.require' => '请输入评论内容',
            ]
        );
        $result = $validate->check($param);
        if (!$result) {
            $this->error(__($validate->getError()));
        }
        $order = Order::get($order_id);
        $order->status = '2';
        $model = new  Review;
        $param['user_id'] = $this->auth->id;
        $param['createtime'] = time();
        $res = $model->allowField(true)->save($param);
        if ($res) {
            $order->save();
            $this->success('成功');
        }
        $this->error('失败');
    }

    /**
     * @Author LK
     * @ApiTitle (评论列表)
     * @ApiMethod (GET)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="limit", type="integer", required=true, sample="", description="每页数量")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function review_list()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $lists = Review::with('user')->where('goods_id', $id)->where('state', '2')->order('id desc')->paginate($limit ?? 10);
        $this->success('', $lists);
    }
    /**
     * @Author LK
     * @ApiTitle (租赁取消)
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, sample="", description="ID")
     * @ApiParams (name="reason", type="integer", required=true, sample="", description="取消原因")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */

    public function cancel_lease()
    {
        $param = request()->param();
        extract($param);
        if (!isset($id) || !$id) {
            $this->error('参数错误');
        }
        $order = Order::get($id);
        if (!$order) {
            $this->error('订单不存在');
        }
        Db::startTrans();
        try {

            $order->cancel_status = '1';
            $order->reason = $reason;
            $order->save();

            Db::commit();
        } catch (\Exception $th) {
            Db::rollback();
            $this->error($th->getMessage());
        }
        $this->success('');
    }
}
